package org.com.ashen.service.util;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

import org.com.ashen.common.exception.BusinessException;
import org.com.ashen.common.exception.ErrorEnum;
import org.com.ashen.dal.dataobject.SaasAppSession;
import org.com.ashen.dal.dataobject.SaasSession;
import org.com.ashen.service.config.JdbcConfig;
import org.com.ashen.service.dao.SaasAppSessionDao;
import org.com.ashen.service.dao.SaasSessionDao;


public class DBUtil {
	private static final String URL_FORMATTER="jdbc:sqlserver://%s:%s;DatabaseName=%s";
	private static String driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
	
	//2000
//	private static final String URL_FORMATTER="jdbc:microsoft:sqlserver://%s:%s;DatabaseName=%s";
//	private static String driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver";
//	private static final String URL_FORMATTER="jdbc:oracle:thin:@%s:%s:%s";
//	private static String driverClassName="oracle.jdbc.driver.OracleDriver";

	private static Connection conn;
	
	public static Connection getConnection(JdbcConfig jdbcConfig, SaasAppSessionDao saasAppSessionDao, String sessionid) {
		try {
			 SaasAppSession saasAppSession = null;
				if(saasAppSessionDao.getAppSession(sessionid) == null){
					throw new BusinessException(ErrorEnum.SESSIONID_ERROR);
				}else {
					saasAppSession = saasAppSessionDao.getAppSession(sessionid);
				}
			Class.forName(driverClassName);
			String url = String.format(URL_FORMATTER,jdbcConfig.dbUrl,jdbcConfig.dbPort,saasAppSession.getDbid());
			conn = DriverManager.getConnection(url, jdbcConfig.dbUser, jdbcConfig.dbPass);
			return conn;
		} catch (Exception e) {
			throw new BusinessException(ErrorEnum.DATASOURCE_NULL);
		}
	}
	
	
	public static Connection getConnection2(JdbcConfig jdbcConfig, SaasSessionDao saasSessionDao, Integer sessionid) {
		try {
			 SaasSession saasSession = null;
				if(saasSessionDao.getSession(sessionid) == null){
					throw new BusinessException(ErrorEnum.SESSIONID_ERROR);
				}else {
					saasSession = saasSessionDao.getSession(sessionid);
				}
			Class.forName(driverClassName);
			String url = String.format(URL_FORMATTER,jdbcConfig.dbUrl,jdbcConfig.dbPort,"RTSaas"+saasSession.getDbid());
			conn = DriverManager.getConnection(url, jdbcConfig.dbUser, jdbcConfig.dbPass);
			return conn;
		} catch (Exception e) {
			throw new BusinessException(ErrorEnum.DATASOURCE_NULL);
		}
	}
	
	public static Connection getRtsaasConnection(JdbcConfig jdbcConfig, SaasAppSessionDao saasAppSessionDao, String sessionid) {
		try {
			 
			Class.forName(driverClassName);
			String url = String.format(URL_FORMATTER,jdbcConfig.dbUrl,jdbcConfig.dbPort,jdbcConfig.dbName);
			conn = DriverManager.getConnection(url, jdbcConfig.dbUser, jdbcConfig.dbPass);
			return conn;
		} catch (Exception e) {
			throw new BusinessException(ErrorEnum.DATASOURCE_NULL);
		}
	}
	
	public static Connection getRtsaasConnection(JdbcConfig jdbcConfig) {
		try {
			 
			Class.forName(driverClassName);
			String url = String.format(URL_FORMATTER,jdbcConfig.dbUrl,jdbcConfig.dbPort,jdbcConfig.dbName);
			conn = DriverManager.getConnection(url, jdbcConfig.dbUser, jdbcConfig.dbPass);
			return conn;
		} catch (Exception e) {
			throw new BusinessException(ErrorEnum.DATASOURCE_NULL);
		}
	}
	
	
	public static Connection getRtsaasConnectionDbid(JdbcConfig jdbcConfig, String Dbid) {
		try {
			 
			Class.forName(driverClassName);
			String url = String.format(URL_FORMATTER,jdbcConfig.dbUrl,jdbcConfig.dbPort,Dbid);
			conn = DriverManager.getConnection(url, jdbcConfig.dbUser, jdbcConfig.dbPass);
			return conn;
		} catch (Exception e) {
			throw new BusinessException(ErrorEnum.DATASOURCE_NULL);
		}
	}
	
	
	public static Connection getRtsaasConnection2(JdbcParam jdbcConfig) {
		try {
			 
			Class.forName(driverClassName);
			String url = String.format(URL_FORMATTER,jdbcConfig.dbUrl,jdbcConfig.dbPort,jdbcConfig.dbName);
			conn = DriverManager.getConnection(url, jdbcConfig.dbUser, jdbcConfig.dbPass);
			return conn;
		} catch (Exception e) {
			throw new BusinessException(ErrorEnum.DATASOURCE_NULL);
		}
	}
	
	
	public static void closeConnection(Statement stmt, Connection conn) {
		try {
			if (stmt != null && conn != null) {
				stmt.close();
				conn.close();
			}else if (stmt == null && conn != null) {
				conn.close();
			}
		} catch (Exception e) {
		}
	}
	
	public static void closeConnection(CallableStatement stmt, Connection conn) {
		try {
			if (stmt != null && conn != null) {
				stmt.close();
				conn.close();
			}else if (stmt == null && conn != null) {
				conn.close();
			}
		} catch (Exception e) {
		}
	}
}
